package com.bristua.barcode.customer.biz;

import com.bristua.barcode.common.exception.BizException;
import com.bristua.barcode.common.util.DateUtils;
import com.bristua.barcode.common.util.SequenceUtil;
import com.bristua.barcode.customer.dao.TCustomerBindSupplierMapper;
import com.bristua.barcode.customer.enums.CustomerCode;
import com.bristua.barcode.customer.model.TCustomerBindSupplier;
import com.bristua.barcode.customer.model.condition.AddCustomerBindQRCodeParams;
import com.bristua.barcode.customer.model.condition.CustomerBindQRCodeParams;
import com.bristua.barcode.supplier.dao.TQrCodeLimitAmtAdjustmentMapper;
import com.bristua.barcode.supplier.dao.TQrCodeMapper;
import com.bristua.barcode.supplier.model.TQrCode;
import com.bristua.barcode.supplier.model.TQrCodeLimitAmtAdjustment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/8/6 0006.
 */
@Service
@Transactional
public class AddCustomerBindQRCodeBiz {

    @Resource
    private TCustomerBindSupplierMapper tCustomerBindSupplierMapper;

    @Resource
    private TQrCodeMapper tQrCodeMapper ;

    @Resource
    private TQrCodeLimitAmtAdjustmentMapper tQrCodeLimitAmtAdjustmentMapper;


    public void addCustomerBindQRCode(AddCustomerBindQRCodeParams addCustomerBindQRCodeParams){

        if(addCustomerBindQRCodeParams.getCustomerBindQRCodeParamsList()!=null && addCustomerBindQRCodeParams.getCustomerBindQRCodeParamsList().size()>0){
            List<CustomerBindQRCodeParams> customerBindQRCodeParamsList = addCustomerBindQRCodeParams.getCustomerBindQRCodeParamsList();
            List<TCustomerBindSupplier> tCustomerBindSupplierList = new ArrayList<>();
            List<TQrCodeLimitAmtAdjustment> tQrCodeLimitAmtAdjustmentList = new ArrayList<>();
            for(CustomerBindQRCodeParams customerBindQRCodeParams : customerBindQRCodeParamsList){
                TQrCode queryTQrCode = tQrCodeMapper.selectByPrimaryKey(customerBindQRCodeParams.getQrCodeId());
                if(queryTQrCode!=null && queryTQrCode.getIsBind().intValue()==1){
                    throw new BizException(CustomerCode.QRCODE_IS_BIND.getCode(),CustomerCode.QRCODE_IS_BIND.getMsg());
                }
                TCustomerBindSupplier tCustomerBindSupplier = new TCustomerBindSupplier();
                tCustomerBindSupplier.setCustomerId(addCustomerBindQRCodeParams.getCustomerId());
                tCustomerBindSupplier.setBindId(SequenceUtil.nextId());
                tCustomerBindSupplier.setSupplierId(customerBindQRCodeParams.getSupplierId());
                tCustomerBindSupplier.setQrCodeId(customerBindQRCodeParams.getQrCodeId());
                tCustomerBindSupplierList.add(tCustomerBindSupplier);
                TQrCode tQrCode = new TQrCode();
                tQrCode.setQrCodeId(customerBindQRCodeParams.getQrCodeId());
                tQrCode.setIsBind(1);
                tQrCode.setBindTime(DateUtils.getShowDateTime());
                tQrCodeMapper.updateByPrimaryKeySelective(tQrCode);

                TQrCodeLimitAmtAdjustment tQrCodeLimitAmtAdjustment = new TQrCodeLimitAmtAdjustment();
                tQrCodeLimitAmtAdjustment.setQrCodeLimtAmtId(SequenceUtil.nextId());
                tQrCodeLimitAmtAdjustment.setLimitAmt((new BigDecimal(customerBindQRCodeParams.getLimitAmt()).multiply(new BigDecimal(100))).longValue());
                tQrCodeLimitAmtAdjustment.setCreateTime(DateUtils.getShowDateTime());
                tQrCodeLimitAmtAdjustment.setFlowTotalAmt(0L);
                tQrCodeLimitAmtAdjustment.setCustomerId(addCustomerBindQRCodeParams.getCustomerId());
                tQrCodeLimitAmtAdjustment.setQrCodeId(customerBindQRCodeParams.getQrCodeId());
                tQrCodeLimitAmtAdjustmentList.add(tQrCodeLimitAmtAdjustment);

            }
            if(tCustomerBindSupplierList.size()>0){
                tCustomerBindSupplierMapper.batchInsert(tCustomerBindSupplierList);
            }

            if(tQrCodeLimitAmtAdjustmentList.size()>0){
                tQrCodeLimitAmtAdjustmentMapper.batchInsert(tQrCodeLimitAmtAdjustmentList);
            }
        }
    }

}
